package WebRoot.demo3.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.hoyi.dishop.Hoyipage;
import org.hoyi.disptachs.model.JsonModel;
import org.hoyi.nosql.redis.HoyiRedisCtrls;
import org.hoyi.nosql.redis.RedisObjects;
import org.hoyi.util.JsonUtil;
import org.hoyi.wb.comment.RequestMode;
import org.hoyi.wb.comment.RequestType;
import org.hoyi.wb.comment.cache.redis.RedisCache;

import net.sf.json.JSONSerializer;

public class RedisDemo extends Hoyipage {

	/**
	 * redis 储存 key value
	 */
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void testRedis() {
		RedisObjects _redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();
		_redisObjects.set("aa", "bb");
		String val = _redisObjects.get("aa");
		HoyiRedisCtrls.returnRedisres(_redisObjects);
		this.WriteUTF8JSONDATAMSG(1, "存储到aa的值为" + val, "");
	}

	/**
	 * redis 方法缓存
	 */
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	@RedisCache(CachKey = "testMethodRedis", expiress = 1000)
	public String testMethodRedis() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("user", "xiaoming");
		this.WriteUTF8JSONDATAMSG(1, "success", map);
		JsonModel json = new JsonModel(1, "success");
		json.setData(map);
		return JSONSerializer.toJSON(json, JsonUtil.Config).toString();
	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void lpush(String key, String value) {
		RedisObjects redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();
		long result = redisObjects.lpush(key, value);
		HoyiRedisCtrls.returnRedisres(redisObjects);

		if (result > 0) {
			this.WriteUTF8JSONDATAMSG(1, "msg", "success!");
		} else {
			this.WriteUTF8JSONDATAMSG(0, "msg", "fail!");
		}

	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void lpop(String key) {
		RedisObjects redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();

		String lpopV = redisObjects.lpop(key, "");

		HoyiRedisCtrls.returnRedisres(redisObjects);
		this.WriteUTF8JSONDATAMSG(1, "msg", lpopV);
	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void lrange(String key, String start, String end) {
		RedisObjects redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();
		List<String> list = redisObjects.lrange(key, Long.valueOf(start), Long.valueOf(end));
		HoyiRedisCtrls.returnRedisres(redisObjects);
		this.WriteUTF8JSONDATAMSG(1, "msg", list);
	}

	/**
	 * 有序集合
	 * 
	 * @param key
	 * @param score
	 * @param value
	 */
	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void zadd(String key, String score, String value) {
		RedisObjects redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();

		redisObjects.zadd(key, Double.valueOf(score), value);

		HoyiRedisCtrls.returnRedisres(redisObjects);
		this.WriteUTF8JSONDATAMSG(1, "msg", "");
	}

	@RequestMode(MODE = { RequestType.GET, RequestType.POST })
	public void lrangeByScore(String key, String start, String end) {
		RedisObjects redisObjects = HoyiRedisCtrls.NEWCS().GetRedis();
		Set<String> set = redisObjects.zrangeByScore(key, 0.5, 1000, Integer.valueOf(start), Integer.valueOf(end));
		HoyiRedisCtrls.returnRedisres(redisObjects);
		this.WriteUTF8JSONDATAMSG(1, "msg", set);
	}
}
